#!/bin/sh
set -e

SELFDIR=`dirname "$0"`
SELFDIR=`cd "$SELFDIR" && pwd`

usage()
{
	echo "Usage: docker-bash <CONTAINER_ID> [COMMAND...]"
	echo "Login to a Baseimage-based Docker container using nsenter." \
		"If COMMAND is not given, opens an interactive shell." \
		"Otherwise, runs COMMAND inside the container."
}

if test $# = 0; then
	usage
	exit
fi

CONTAINER_ID="$1"
shift

PID=`docker inspect -f "{{ .State.Pid }}" "$CONTAINER_ID"`
if test $# = 0; then
	exec "$SELFDIR/baseimage-docker-nsenter" --target "$PID" --mount --uts --ipc --net --pid -- /bin/bash -l
else
	exec "$SELFDIR/baseimage-docker-nsenter" --target "$PID" --mount --uts --ipc --net --pid -- "$@"
fi

